package collection;

import day02.Test;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author DBX
 * @effect 进阶
 *   实现读取浏览器发送过来的一行字符串
 *
 *   可以参考day05晚课练习
 *   浏览器发送过来的内容的特点:
 *   1:内容全是英文，数字和符号，都是单字节编码的字符
 *   2:每一行字符串后面跟着两个不可见的字符:回车符和换行符
 *     而这两个字符对应的编码数字是13,10
 *     就好比'a'对应的编码数字是97
 *
 *   因此可以连续读取若干字符，如果上次读取的字符==13
 *   本次读取的字符==10就可以停止，并将之前的字符组成一个
 *   字符串并输出
 * @date 2021/09/04/19:52
 */
public class Test7 {
    private ServerSocket serverSocket;
    private Socket socket;
    public Test7(){
        try {
            serverSocket = new ServerSocket(8088);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    InputStream is;
    public void start(){
        try {
            socket = serverSocket.accept();
            System.out.println("连接成功");
            is = socket.getInputStream();

            int b ;
            int c = 0;
            StringBuilder sb = new StringBuilder();
            while ((b = is.read())!=-1){
                if (b==10&&c==13){
                    System.out.println();
                    System.out.print(sb.toString());
                    sb = new StringBuilder();
                }else {
                    sb.append((char)b);
                }c = b;
            }
            System.out.println("--->");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        Test7 t = new Test7();
        t.start();




    }
}
